<?php
require_once '../libs/app.config.php';
session_start();
if($_SESSION['LoginId'] == false){
	header("Location:login.php");
}
    ini_set ('memory_limit','128M');
	$upload = new UPImages("../public/images2/","","","");

	if($id=="")
	{
		$sql="select * from adv_position order by id desc";
		$query=$tpm->query($sql);
		while($row=$tpm->fetch_array($query))
		{
			$advcat[]=$row;
		}
	}
	//添加
	if($_GET['act']=='add')
	{		$file=$_FILES;
			$_FILES['fileDoc']['name']=$file['fileDoc']['name'];
			$_FILES['fileDoc']['type']=$file['fileDoc']['type'];
			$_FILES['fileDoc']['tmp_name']=$file['fileDoc']['tmp_name'];
			$_FILES['fileDoc']['error']=$file['fileDoc']['error'];
			$_FILES['fileDoc']['size']=$file['fileDoc']['size'];
			$upload->maxWidth=500000000;
			$upload->maxHeight=500000000;
			$img=$upload->upLoad("fileDoc");

			$add=array("position_id"=>$position_id,
					   "adv_name"=>$adv_name,
					   "adv_name_en"=>$adv_name_en,
					   "adv_desc"=>$adv_desc,
					   "adv_desc_en"=>$adv_desc_en,
					   "adv_link"=>$adv_link,
					   "content"=>$img
					  );
		  $result=$tpm->insert("adv_content",$add);
		  if($result>0)
		  echo "<script>alert('Add successfully!');location.href=('adv_list.php')</script>";
	}
	//修改
	if($id!="")
	{
	$sql="SELECT * FROM `adv_position`";
	$query=$tpm->query($sql);$show="";
	while($row=$tpm->fetch_array($query))
	{
		$sqls="SELECT * FROM `adv_content` where id='".$_GET['id']."'";
		$querys=$tpm->query($sqls);
		$rows=$tpm->fetch_array($querys);
		if($row['id']==$rows['position_id']){
			$show.="<option value='".$row['id']."' selected>".$row['position_name']."</option>";}
		else
		   {$show.="<option value='".$row['id']."'>".$row['position_name']."</option>";}
	}

$sql1="select * from adv_content where id='".$_GET['id']."'";
$query1=$tpm->query($sql1);
$rows=$tpm->fetch_array($query1);
$list=array("adv_name"=>$rows['adv_name'],
			"adv_name_en"=>$rows['adv_name_en'],
			"adv_desc"=>$rows['adv_desc'],
			"adv_desc_en"=>$rows['adv_desc_en'],
			"adv_link"=>$rows['adv_link'],
			"content"=>"../public/images2/".$rows['content'],
			"last_update_time"=>date('Y-m-d H:i:s') );
	}


if($_POST['submit']){
if($_GET['act']=='edit')
{


if(!empty($_FILES['fileDoc']['name']))
{
 $file=$_FILES;
		   $_FILES['fileDoc']['name']=$file['fileDoc']['name'];
			$_FILES['fileDoc']['type']=$file['fileDoc']['type'];
			$_FILES['fileDoc']['tmp_name']=$file['fileDoc']['tmp_name'];
			$_FILES['fileDoc']['error']=$file['fileDoc']['error'];
			$_FILES['fileDoc']['size']=$file['fileDoc']['size'];
			$upload->maxWidth=500000000;
			$upload->maxHeight=500000000;
		    $img=$upload->upLoad("fileDoc");


    $sql="select content from adv_content where id='".$_POST['id']."'";
	$query=$tpm->query($sql);
	$row=$tpm->fetch_array($query);
	$path="../public/images2/".$row['content'];
	if(file_exists($path)){
		@unlink($path);
		clearstatcache();
		}
	 $update=array("adv_name"=>$adv_name,
					   "adv_name_en"=>$adv_name_en,
					   "adv_desc"=>$adv_desc,
					   "adv_desc_en"=>$adv_desc_en,
				   "adv_link"=>$adv_link,
				   "content"=>$img
				   );
	 $where="id='".$_POST['id']."'";
	 $result=$tpm->update("adv_content",$update,$where);
	 if($result>0)
	 echo "<script>alert('Edit successfully!');location.href=('adv_list.php');</script>";

}
else
{
	 $update=array("adv_name"=>$adv_name,
					   "adv_name_en"=>$adv_name_en,
					   "adv_desc"=>$adv_desc,
					   "adv_desc_en"=>$adv_desc_en,
				   "adv_link"=>$adv_link
				   );
	 $where="id='".$_POST['id']."'";
	 $result=$tpm->update("adv_content",$update,$where);
	 if($result>0)
	 echo "<script>alert('Edit successfully!');location.href=('adv_list.php');</script>";
}

}
}

require_once(INCLUDE_PATH.'template.inc.php');  //模板类及对象$tpl的加载
$tpl->assign("list",$list);
$tpl->assign("id",$id);
$tpl->assign("show",$show);
$tpl->assign("advcat",$advcat);
$tpl->display("adv_add.html");
?>

